package com.qa.dom.config.shiro;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.qa.dom.config.redis.RedisConfig;
import com.qa.dom.entity.Usertable;
import com.qa.dom.service.IFUserService;
import com.qa.dom.service.IUsertableService;
import com.qa.dom.util.Constants;
import com.qa.dom.util.MD5;
import com.qa.dom.util.NTESUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;

/**
 * @author 张成基
 * @version V1.0
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: TODO
 * @date ${date} ${time}
 */
@Component
public class UsertableRealm extends AuthorizingRealm {

    private static final transient Logger logger = LoggerFactory.getLogger(UsertableRealm.class);

    @Autowired
    private IUsertableService iUsertableService;

    @Autowired
    public StringRedisTemplate stringRedisTemplate;

    @Autowired
    public RedisTemplate redisTemplate;

    @Autowired
    public RedisConfig redisConfig;

    //用户登录次数计数  redisKey 前缀
    private String SHIRO_LOGIN_COUNT = "shiro_login_teach_count_";

    //用户登录是否被锁定    一小时 redisKey 前缀
    private String SHIRO_IS_LOCK = "shiro_is_lock_";



    //身份认证配置
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
        logger.info("身份认证————————————————————>TeacherRealm.doGetAuthorizationInfo()");
        //获取用户的输入账号
        UsernamePasswordCaptchaToken token = (UsernamePasswordCaptchaToken) authcToken;
        String username = token.getUsername();
        String password = String.valueOf(token.getPassword());
        String captcha = token.getCaptcha();

       /* if(com.baomidou.mybatisplus.toolkit.StringUtils.isNotEmpty(captcha)){
            if(!NTESUtils.checkMsg(username,captcha)){
                throw new CaptchaException("验证码错误");
            }
        }*/

        //查询当前用户
        EntityWrapper<Usertable> wrapper=new EntityWrapper<Usertable>();
        wrapper.eq("phone",username);
        Usertable user=iUsertableService.selectOne(wrapper);
        if(user==null){
            return null;
        }
        ByteSource credentialsSalt=ByteSource.Util.bytes(username);
        return new SimpleAuthenticationInfo(user,password,credentialsSalt,getName());
    }


    //权限配置(授权){暂时没有用到}
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        logger.info("权限配置————————————————————>TeacherRealm.doGetAuthorizationInfo()");
        //获取用户信息
        Usertable user= (Usertable) principals.getPrimaryPrincipal();
        SimpleAuthorizationInfo authenticationInfo =  new SimpleAuthorizationInfo();
     //   appointclassMapper.
        Set<String> roleSet = new HashSet<String>();
        roleSet.add("100004");
        authenticationInfo.setRoles(roleSet);
        Set<String> permissionSet = new HashSet<String>();
        permissionSet.add("权限添加");
        permissionSet.add("权限删除");
        authenticationInfo.setStringPermissions(permissionSet);
        return authenticationInfo;
    }

}
